﻿ 
using SHKF.B2C.Model.Dto.Sale;
using SHKF.B2C.Model.Entity.Sale; 
using SqlSugar;
using System;
using System.Collections.Generic; 

namespace SHKF.B2C.BLL.LM
{
    public class SaleContentBLL : LMBLL
    {
        private readonly IDBLL iDBLL;

        public SaleContentBLL(SqlSugarClient db, IDBLL iDBLL) : base(db)
        { 
            this.iDBLL = iDBLL; 
        }

        public long Save(SaleContentEntity sale, List<SaleDetailEntity> details)
        { 
            long SN = 0;

            try
            {
                this.db.BeginTran();

                if (sale.SN == 0)
                {
                    //创建定单
                    SaleID saleID = this.iDBLL.GetSaleID(sale.CompanySN, sale.ShopSN);
                    sale.ID = saleID.ID;
                    sale.Num = saleID.Num;

                    SN = this.db
                        .Insertable<SaleContentEntity>(sale)
                        .ExecuteReturnBigIdentity();

                    foreach (SaleDetailEntity detail in details)
                    {
                        detail.FKSN = SN;

                        detail.SN = this.db.Insertable<SaleDetailEntity>(detail)
                            .ExecuteReturnBigIdentity();

                        //更新库存
                        //WareHouseDataEntity wareHouse = this.db.Queryable<WareHouseDataEntity>()
                        //    .Where(oo => oo.CompanySN == sale.CompanySN
                        //        && oo.ShopSN == sale.ShopSN
                        //        && oo.ProductSN == detail.ProductSN)
                        //    .First();

                        //if (wareHouse != null)
                        //{
                        //    wareHouse.Qty -= detail.Qty;
                        //    int result = this.db
                        //                    .Updateable<WareHouseDataEntity>(wareHouse) 
                        //                    .ExecuteCommand();
                        //}
                    }
                }
                else
                {
                    this.db.Updateable<SaleContentEntity>(sale)
                        .ExecuteCommand();

                    foreach (SaleDetailEntity detail in details)
                    {
                        this.db.Updateable<SaleDetailEntity>(detail)
                        .ExecuteCommand();
                    }
                }

                this.db.CommitTran();

            }
            catch (Exception ex)
            {
                this.db.RollbackTran();
                throw ex;
            }
          
            return SN;
        }

        public SaleContentInfo Query(long SN)
        {
            SaleContentInfo saleContent  = this.db
              .Queryable<SaleContentEntity>() 
              .Select<SaleContentInfo>()
              .Where(oo=>oo.SN == SN)
              .First();

            return saleContent;
        }

        public List<SaleContentInfo> Query(int ShopSN, DateTime StartDate, DateTime EndDate, int Status)
        {
            List<SaleContentInfo> saleContents = new List<SaleContentInfo>();

            var cmd = this.db
                .Queryable<SaleContentEntity>();

            if (ShopSN > 0)
            {
                cmd.Where(oo => oo.ShopSN == ShopSN);
            }

            cmd.Where(oo => oo.pDate >= StartDate && oo.pDate <= EndDate);

            if (Status >= 0)
            {
                cmd.Where(oo => oo.Status == Status);
            }

            saleContents = cmd.ToList(a=>new SaleContentInfo());

            return saleContents;
        }

        /// <summary>
        /// 查询门店未接单总数
        /// </summary>
        /// <param name="ShopSN"></param>
        /// <returns></returns>
        public long QueryUnCount(int ShopSN)
        {
           long count = this.db
                .Queryable<SaleContentEntity>()
                .Where(oo => oo.ShopSN == ShopSN && oo.Status == 0)
                .Count();

            return count;
        }

        public List<V_SaleDetail> QueryDetial(long fksn)
        { 
            string cmdText = "select a.*, ";
            cmdText += "p.ID, p.Made, p.Spec, p.Unit, p.Model, p.LiceseId, p.Upc, ";
            cmdText += "p.CompanySN, p.ShopSN,p.ProductName, p.is_sold_out, p.Manufacturer,p.Validate,p.CommonTitle, p.ConditionOfStorage ";
            cmdText += "from SM_SaleDetail a";
            cmdText += $" inner join PtE_Center.dbo.ProductInfo p on a.ProductSN = p.ProductSN where a.FKSN = {fksn}";

            return this.db.SqlQueryable<V_SaleDetail>(cmdText).ToList();
 
        }
    }
}
